<template>
  <div>
    <!--登陆-->
    <el-dialog :visible.sync="dialogFlag" width="24%" :before-close="handk">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item>
          <h2>登陆</h2>
        </el-form-item>
        <el-form-item prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入账号"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="hands">登陆</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import loginRegister from "../api/loginRegister";
export default {
  props: ["dialogFlag"],
  components: {},
  name: "LoginModule",
  data() {
    var userName = (rule, value, cb) => {
      const regUserName = /^[a-zA-Z]\w{5,16}$/;
      if (regUserName.test(value)) {
        return cb();
      }
      cb(new Error("字母开头,长度5-16之间,允许字母数字下划线"));
    };

    var password = (rule, value, cb) => {
      const regPassword = /^[a-zA-Z]\w{6,18}$/;
      if (regPassword.test(value)) {
        return cb();
      }
      cb(new Error("字母开头,长度6-18之间,允许字母数字下划线"));
    };
    return {
      ruleForm: {
        userName: "",
        password: "",
      },

      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            validator: userName,
            message: "字母开头,长度5-16之间,允许字母数字下划线",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            validator: password,
            message: "字母开头,长度6-18之间,允许字母数字下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    handk(done) {
      this.$emit("elflagOne");
    },
    async hands() {
      let { data: res } = await loginRegister.getLogin({
        userName: this.ruleForm.userName,
        password: this.ruleForm.password,
      });
      console.log(res);

      if (res.code === "001") {
        this.$notify({
          message: res.msg,
          type: "success",
        });
      }
      this.$store.commit("user", res.user.userName);
      this.ruleForm = "";//清空数据
      this.$emit("elflagTwo");
      if (res.code !== "001") {
        this.$notify({
          message: res.msg,
          type: "error",
        });
      }
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-button {
    width: 100%;
  }
  h2 {
    text-align: center;
  }
}
</style>